package TestSort;

import com.sun.org.apache.xalan.internal.res.XSLTErrorResources_zh_TW;

import java.util.Random;

public class HeapSort {
    public static int[] arr = new int[1_000];

    public HeapSort () {
        Random random = new Random();
        int i = 0;
        while (i < arr.length) {
            arr[i] = random.nextInt(1_000);
            i++;
        }
    }

    public static void swap (int[] arr, int a, int b) {
        int tmp = arr[a];
        arr[a] = arr[b];
        arr[b] = tmp;
    }

    public static void heapSort (int[] arr) {
        createBigRootHeap(arr);
        int end = arr.length - 1;
        while (end >= 0) {
            swap(arr, end, 0);
            shiftDown(arr, 0, end);
            end--;
        }
    }

    public static void createBigRootHeap (int[] arr) {
        int parent = (arr.length - 2) / 2;
        while (parent >= 0) {
            shiftDown(arr, parent, arr.length);
            parent--;
        }
    }

    public static void createSmallRootHeap (int[] arr) {
        int parent = (arr.length - 2) / 2;
        while (parent >= 0) {
            shiftDown2(arr, parent, arr.length);
            parent--;
        }
    }

    public static void shiftDown (int[] arr, int parent, int len) {
        int child = parent * 2 + 1;
        while (child < len) {
            if (child + 1 < len && arr[child + 1] > arr[child]) {
                child++;
            }
            if (arr[child] > arr[parent]) {
                swap(arr, child, parent);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    public static long testHeapSort (int[] arr) {
        long startTime = System.currentTimeMillis();
        heapSort(arr);
        long endTime = System.currentTimeMillis();
        for (int x:
                arr) {
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println("============");
        return endTime - startTime;
    }

    public static void heapSort2 (int[] arr) {
        createSmallRootHeap(arr);
        int end = arr.length - 1;
        while (end >= 0) {
            swap(arr, end, 0);
            /**
             * 主要是这里导致shiftDown中需要一个len
             * 而不是每次的终止条件都是arr.length
             * 参数改成arr.length马上排序就出错
             * swap就是把最小的（堆顶）放到堆尾
             * 然后end--就可以保证末尾的值一直最小，不会被再排序
             */
            shiftDown2(arr, 0, end);
            end--;
        }
    }

    public static void shiftDown2 (int[] arr, int parent, int len) {
        int child = parent * 2 + 1;
        while (child < len) {
            if (child + 1 < len && arr[child + 1] < arr[child]) {
                child++;
            }
            if (arr[child] < arr[parent]) {
                swap(arr, child, parent);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    public static void main(String[] args) {
        HeapSort heap = new HeapSort();
        long startTime = System.currentTimeMillis();
        heap.heapSort2(arr);
        long endTime = System.currentTimeMillis();
        for (int x:
                arr) {
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println("============");
        long ret = endTime - startTime;
        System.out.println(ret);
    }

    public static void main1(String[] args) {
        HeapSort heap = new HeapSort();
        System.out.println(heap.testHeapSort(arr));
    }
}
